package com.ks.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ks.dao.ContentQuestionDao;
import com.ks.entity.ContentQuestion;
import com.ks.entity.Question;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.base.BaseService;
import com.ks.dao.QuestionDao;

import javax.persistence.criteria.CriteriaBuilder;

@Service("questionService")
public class QuestionService<T> extends BaseService<T> {
    private final static Logger log = Logger.getLogger(QuestionService.class);

    @Autowired
    private QuestionDao<T> dao;
    @Autowired
    private ContentQuestionDao<T> contentQuestionDao;

    public QuestionDao<T> getDao() {
        return dao;
    }

    public List<Question> queryQuestions(Object o) {
        // TODO Auto-generated method stub
        return dao.queryQuestions(o);
    }

    public List<Question> quertListByIdType(Object o) {
        return dao.quertListByIdType(o);
    }

    public List<Question> findAll(Object o) {
        return dao.findAll(o);
    }

    public Question findAnswerById(Object o) {
        return dao.findAnswerById(o);
    }

    public Map<String, Object> practicexercise(String contentId) {
        //先从content_quertion 取出知识点所有题
        Map<String, Object> map = new HashMap<>();
        List<Question> singlelist = new ArrayList<>();//单选
        List<Question> mutilist = new ArrayList<>();//多选
        List<Question> judgelist = new ArrayList<>();//判断
        List<ContentQuestion> contentQuestions = contentQuestionDao.findByContentId(contentId);
        for (ContentQuestion contentQuestion : contentQuestions) {
            Question question = (Question) dao.queryById(contentQuestion.getQuestion_id());
            if (question.getQuestiontype_id() == 1) {
                singlelist.add(question);
            }
            if (question.getQuestiontype_id() == 2) {
                mutilist.add(question);
            }
            if (question.getQuestiontype_id() == 3) {
                judgelist.add(question);
            }
        }
        map.put("singlelist", singlelist);
        map.put("mutilist", mutilist);
        map.put("judgelist", judgelist);
        map.put("count", contentQuestions.size());
        return map;
    }

    public Map<String, Object> onlinetesting(Integer userId, Integer questiontypeId) {
        Map<String, Object> context = new HashMap<>();
        context.put("userId", userId);
        context.put("questiontypeId", questiontypeId);
        List<Question> singlelist = new ArrayList<>();//单选
        List<Question> mutilist = new ArrayList<>();//多选
        List<Question> judgelist = new ArrayList<>();//判断
        List<Question> questions = dao.findByJoinSQuestion(context);
        for (Question question : questions) {
            if (question.getQuestiontype_id() == 1) {
                singlelist.add(question);
            }
            if (question.getQuestiontype_id() == 2) {
                mutilist.add(question);
            }
            if (question.getQuestiontype_id() == 3) {
                judgelist.add(question);
            }
        }
        if (questiontypeId == 1) {
            context.put("singlelist", singlelist);
            context.put("count", singlelist.size());
        }
        if (questiontypeId == 2) {
            context.put("mutilist", mutilist);
            context.put("count", mutilist.size());
        }
        if (questiontypeId == 3) {
            context.put("judgelist", judgelist);
            context.put("count", judgelist.size());
        }
        return context;
    }

    /**
     * @param userId  用户id
     * @param paperId 试题类型id 单选1 多选2 判断3
     * @return 返回map集合
     */
    public Map<String, Object> onlinetestingAjax(Integer userId, Integer paperId) {
        Map<String, Object> context = new HashMap<>();
        context.put("userId", userId);
        context.put("questiontypeId", paperId);
        List<Question> questions = dao.findByJoinSQuestion(context);
        if (paperId == 1) {
            context.put("count", questions.size());
        }
        if (paperId == 2) {
            context.put("count", questions.size());
        }
        if (paperId == 3) {
            context.put("count", questions.size());
        }
        context.put("tips", "没有错题可练");
        return context;
    }
}
